/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package filesecurity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author lehoangkhanh
 */
public class VerifyForm extends javax.swing.JInternalFrame {

    /**
     * Creates new form VerifyForm
     */
    String fileName = null;
    String filePath = null;
    String folderPath = null;
    MainFrame rootFrame = null;
    public VerifyForm(MainFrame root) {
        initComponents();
        rootFrame = root;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        signfile = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Verify Form");

        jLabel1.setText("Sign File:");

        signfile.setText("...");
        signfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signfileActionPerformed(evt);
            }
        });

        jButton2.setText("OK<<");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(signfile, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(54, 54, 54)))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(signfile))
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void signfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signfileActionPerformed
        // TODO add your handling code here:
        JFileChooser file = new JFileChooser();
        int op = file.showOpenDialog(null);
        if(op == JFileChooser.APPROVE_OPTION){
            filePath = file.getSelectedFile().toPath().toString();
            fileName = file.getSelectedFile().getName().toString();
            folderPath = file.getSelectedFile().getParent().toString();
            signfile.setText("Just Selected...");
            signfile.setToolTipText(filePath);
        }
    }//GEN-LAST:event_signfileActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if(fileName==null || fileName.equals("")){
            JOptionPane.showMessageDialog(rootPane, "Please select location of output file!!!");
            return;
        }
        byte[] file = Utils.readFile(rootFrame.getFilePath());
        byte[] sign = Utils.readFile(filePath);
        HashMap<String , User> list = rootFrame.getUserList();
        Set s = list.keySet();
        Iterator i = s.iterator();
        while(i.hasNext()){
            String email  = (String)i.next();
            User user = list.get(email);
            boolean state = DigitalSign.verify(file, sign, user.getPublicKey());
            if(state == true){
                JOptionPane.showMessageDialog(rootPane, "File is valid. The owner is "+ user.getEmail());
                this.setVisible(false);
                return;
            }
        }
        JOptionPane.showMessageDialog(rootPane, "File is invalid!!!");
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton signfile;
    // End of variables declaration//GEN-END:variables
}
